package com.lf.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.lf.api.Constants;
import com.lf.api.po.dao.SubjectMapper;
import com.lf.api.po.dao.UserCommentMapper;
import com.lf.api.po.dao.UserGreateMapper;
import com.lf.api.po.dao.UserInfoMapper;
import com.lf.api.po.dao.UserReplyMapper;
import com.lf.api.po.domain.UserComment;
import com.lf.api.po.domain.UserGreate;
import com.lf.api.po.domain.UserReply;
import com.lf.api.service.CommentReplyService;
import com.lf.api.vo.CommentVO;
import com.lf.api.vo.ReplyVO;

@Service
public class CommentReplyServiceImpl implements CommentReplyService {

	@Autowired
	UserCommentMapper userCommentMapper;
	@Autowired
	UserReplyMapper userReplyMapper;
	@Autowired
	SubjectMapper subjectMapper;
	@Autowired
	UserInfoMapper userInfoMapper;
	@Autowired
	UserGreateMapper userGreateMapper;

	@Override
	public void saveComment(UserComment comment) {
		userCommentMapper.insert(comment);
	}

	@Override
	public void saveReply(UserReply reply) {
		userReplyMapper.insert(reply);
	}

	@Override
	public void saveGreate(UserGreate greate) {
		// 1 保存点赞
		userGreateMapper.insert(greate);
		// 2 增加评论点赞数
		if (greate.getObjectType().shortValue() == Constants.GreateType.COMMENT) {
			userCommentMapper.addOneGreateNum(greate.getObjectId());
		} else if (greate.getObjectType().shortValue() == Constants.GreateType.REPLY) {
			userReplyMapper.addOneGreateNum(greate.getObjectId());
		} else if (greate.getObjectType().shortValue() == Constants.GreateType.SUBJECT) {
			subjectMapper.addOneGreateNum(greate.getObjectId());
		}
	}

	@Override
	public List<CommentVO> queryCommentBySubjectId(Long subjectId, int pageNo, int pageSize) {
		if(pageNo < 0) pageNo = 0;
		if(pageSize <=0 ) pageSize = 20;
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("start", (pageNo - 1) * pageSize);
		//pageSize + 1 用于前端判断是否有下一页
		param.put("limit", pageSize + 1);
		param.put("subjectId", subjectId);
		List<CommentVO> comments = userCommentMapper.selectByPage(param);
		if(CollectionUtils.isEmpty(comments)){
			return comments;
		}
		
		for(CommentVO comment : comments){
			List<ReplyVO> replys = queryReplyByCommentId(comment.getId());
			comment.setReplys(replys);
		}
		return comments;
	}
	
	@Override
	public  List<ReplyVO> queryReplyByCommentId(Long commentId) {
		List<ReplyVO> replyList = new ArrayList<ReplyVO>();
		List<ReplyVO> parentReplyList = new ArrayList<ReplyVO>();
		List<ReplyVO> replys = userReplyMapper.selectByCommentId(commentId);
		if(CollectionUtils.isEmpty(replys)){
			return new ArrayList<ReplyVO>();
		}
		for(ReplyVO reply : replys){
			if(reply.getReplyType() == Constants.ReplyType.REPLY_COMMENT ){//评论的回复
				replyList.add(reply);
				parentReplyList.add(reply);
			}else{//回复的回复
				boolean foundParent = false;
                if (replyList.size() > 0) {
                    for (ReplyVO parent : parentReplyList) {
                        if (parent.getId().equals(reply.getReplyId())) {
                            if (parent.getReplys() == null) {
                                parent.setReplys(new ArrayList<ReplyVO>());
                            }
                            parent.getReplys().add(reply);
                            parentReplyList.add(reply);
                            foundParent = true;
                            break;
                        }
                    }
                }
                if (!foundParent) {
                    throw new RuntimeException("sort reply error,should not go here.");
                }
			}
		}
		return replyList;
	}

	
}
